home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGBLER / MATHASM.LZH / FLOAT.ASM < prev    next >
Assembly Source File  |  1985-12-02  |  10KB  |  181 lines

  1. ;*******************************************************************
  2. ;                        FLOATING POINT
  3. ;                    ADDITION AND SUBTRACTION
  4. ;                           ROUTINE
  5. ;*******************************************************************
  6. FLOAT_A  PROC      NEAR
  7.                                ;entry point for subtraction
  8. SUB_F:   MOV       BX,0080H    ;set mask to reverse sign of operand2
  9.          JMP SHORT F1          ;and go to main routine       
  10. ;                                                           
  11.                                ;entry point for addition
  12. ADD_F:   SUB       BX,BX       ;clear mask register BX                
  13. ;
  14.                                ;get operand1 from address in DI
  15. F1:      PUSH      SI          ;save index registers       
  16.          PUSH      DI          ;                              
  17.          MOV       DX,[DI]+2   ;exponent goes in DH--mantissa in DL
  18.          MOV       AX,[DI]     ;and AX
  19.  
  20.                                ;get operand2 from address in SI
  21.          XOR       BX,[SI]+2   ;exponent goes in BH--mantissa in BL
  22.          MOV       CX,[SI]     ;and CX                          
  23.                 
  24.                 ;***************************************************
  25.                 ;       Floating point addition routine begins here
  26.                 ;       operands are in the following format:
  27.                 ;              DH      DL bit 7      DL AX
  28.                 ; Operand1  exponent    sign       mantissa                     
  29.                 ;
  30.                 ;              BH      BL bit 7      BL CX  
  31.                 ; Operand2  exponent    sign       mantissa
  32.                 ;***************************************************
  33.  
  34.                                  ;save signs 
  35.         MOV        SI,10000000B  ;put mask for sign bit is SI 
  36.         MOV        DI,SI         ;and in DI                 
  37.  
  38.         AND        SI,DX         ;put sign of operand1 in SI bit 7  
  39.         AND        DI,BX         ;put sign of operand2 in DI bit 7
  40.  
  41.         CMP        SI,DI         ;                              
  42.         JE         A1            ;if signs are unequal       
  43.         ADD        DI,8000H      ;set bit 15 to 1.  Bit 15 is    
  44.         ADD        SI,8000H      ;addition/subtraction flag 
  45.  
  46. A1:     OR         DL,10000000B  ;restore leading ones       
  47.         OR         BL,10000000B  ;                             
  48.                                  ;put smaller operand in DX:AX
  49.         CMP        DX,BX         ;compare first word              
  50.         JA         A2            ;exchange if DX:AX is larger
  51.         JB         A3            ;                               
  52.  
  53.         CMP        AX,CX         ;equal first words; compare second 
  54.         JA         A2            ;                             
  55.         JB         A3            ;                                
  56.                                  ;operands are equal
  57.         OR         DI,DI         ;if signs are unequal             
  58.         JNS        A3            ;                            
  59.  
  60.         SUB        DX,DX         ;set result in DX:AX to zero      
  61.         MOV        AX,DX         ;                            
  62.         JMP        EXIT          ;and return                     
  63.  
  64. A2:     XCHG       DX,BX         ;exchange operands               
  65.         XCHG       AX,CX         ;                           
  66.         MOV        DI,SI         ;put sign of larger in DI         
  67.  
  68. A3:     MOV        SI,CX         ;larger operand is now in BX:SI    
  69.                                  ;compute exponent difference
  70.         MOV        CL,BH         ;put larger exponent in CL         
  71.         SUB        CL,DH         ;and subtract the smaller exponent
  72.  
  73.                                  ;CL is COUNT of bits to shift DX:AX
  74.         CMP        CL,24         ;if COUNT > 24, smaller operand is
  75.         JLE        A4            ;effectively 0, so               
  76.         MOV        DX,BX         ;put larger operand(result) in DX:AX
  77.         MOV        AX,SI         ;                                  
  78.         JMP        FINISH        ;and finish                   
  79.                                  ;ALIGN fractions (shift smaller right)
  80. A4:     SUB        DH,DH         ;clear DH                          
  81.         MOV        CH,DH         ;clear CH                     
  82.  
  83. A5:     CMP        CL,8          ;                                  
  84.         JL         A6            ;if COUNT >= 8                
  85.                                  ;shift right in byte increments
  86.         OR         CH,DH         ;put trailing bits in CH      
  87.         MOV        DH,AL         ;move leading bits in AL (and any 
  88.                                  ;others that happen to be set) to DH 
  89.         MOV        AL,AH         ;shift upper bytes right          
  90.         MOV        AH,DL         ;                            
  91.         SUB        DL,DL         ;set leading byte to zero        
  92.         SUB        CL,8          ;decrement COUNT            
  93.         JMP  SHORT A5            ;if COUNT >= 8, repeat           
  94.  
  95. A6:     OR        CH,CH          ;if any bits set in CH           
  96.         JZ        A7             ;                           
  97.         OR        CH,80H         ;set sticky bit                   
  98.  
  99. A7:     OR        CL,CL          ;                                 
  100.         JZ        A9             ;if COUNT > 0                
  101.                                  ;shift right COUNT bits
  102. A8:     SHR       DL,1           ;rotate mantissa in DL:AX right    
  103.         RCR       AX,1           ;                             
  104.         RCR       DH,1           ;rotate round bits in DH:CH right 
  105.         RCR       CH,1           ;                            
  106.         DEC       CL             ;decrement COUNT                  
  107.         JNZ       A8             ;repeat if COUNT in CL not zero
  108.  
  109.         MOV       CL,3FH         ;put mask in lower 6 bits of CL  
  110.         AND       CL,DH          ;pick up rounding bits from DH
  111.         AND       DH,0C0H        ;zero out all but two guard bits in DH 
  112.                                  ;operands are now aligned
  113. A9:     OR        DI,DI          ;if signs were the same,i.e., sign
  114.         JS        A10            ;bit = 0, then                   
  115.  
  116.         ADD       AX,SI          ;ADD the two operands              
  117.         ADC       DL,BL          ;                             
  118.         JNC       R0             ;if there was a carry, fraction is 
  119.                                  ;greater than 0, so normalize   
  120.         INC       BH             ;increment exponent                
  121.         JZ        OVER_F         ;jump if exponent overflow    
  122.         RCR       DL,1           ;shift fraction right one bit      
  123.         RCR       AX,1           ;                             
  124.         RCR       DH,1           ;                                 
  125.         JMP SHORT R0             ;                            
  126.                                  ;SUBTRACT the two operands
  127. A10:    XCHG      SI,AX          ;put larger operand in DL:AX      
  128.         XCHG      DL,BL          ;                            
  129.         NEG       DH             ;subtract DH (guard bits)         
  130.         SBB       AX,SI          ;subtract significant portions
  131.         SBB       DL,BL          ;                                 
  132.         JS        R0             ;if DL bit 7 is 1, round     
  133.                                  ;NORMALIZE
  134. A11:    DEC       BH             ;decrement exponent               
  135.         JZ        UNDER_F        ;jump if exponent underflow  
  136.         SHL       DH,1           ;shift mantissa left              
  137.         RCL       AX,1           ;                            
  138.         RCL       DL,1           ;                                 
  139.         OR        DL,DL          ;                            
  140.         JNS       A11            ;repeat if still not normalized  
  141.  
  142. RO:     OR      CH,CL            ;set sticky bit before rounding  
  143.         JZ        R1             ;if any trailing bits in CX 
  144.         OR        DH,01000000B   ;set sticky bit in DH         
  145.                                  ;ROUND normalized result
  146. R1:     OR        DH,DH          ;if guard bit in DH is 0,        
  147.         JNS       FINISH         ;rounding is not necessary   
  148.                                  ;guard bit is on
  149.         TEST      AL,1           ;if fraction is odd, then         
  150.         JNZ       R2             ;round it                    
  151.  
  152.         AND       DH,7FH         ;check trailing bits in DH and CX 
  153.         JZ        FINISH         ;if all zeros, rounding not needed
  154.  
  155. R2:     ADD       AX,1           ;round by adding one             
  156.         ADC       DL,0           ;                           
  157.         JNC       FINISH         ;rounding may denormalize mantissa
  158.  
  159.         RCR       DL,1           ;normalize by shifting right one 
  160.         RCR       AX,1           ;                           
  161.         INC       BH             ;increment exponent              
  162.         JZ        OVER_F         ;jump if exponent overflow  
  163.  
  164. FINISH: AND       DL,7FH         ;set sign bit to 0               
  165.         OR        DX,DI          ;restore sign from bit 7 of DI
  166.         MOV       DH,BH          ;put exponent in DH              
  167.         SUB       BX,BX          ;set status=0 -- OK         
  168. EXIT:   POP       DI             ;restore index registers         
  169.         POP       SI             ;                           
  170.         RET
  171.  
  172. OVER_F: MOV       DX,0FF7FH ;here for overflow--set result in DX:AX 
  173.         MOV       AX,0FFFFH ;to maximum possible value with positive 
  174.         OR        DX,DI     ;sign. Restore actual result sign.
  175.         MOV       BX,1      ;set status=1 -- overflow      
  176.         JMP SHORT EXIT      ;                                 
  177. UNDER_F: SUB      DX,DX     ;set result in DX:AX to zero   
  178.         MOV       AX,DX     ;                                   
  179.         MOV       BX,-1     ;set status=-1 -- underflow      
  180.         JMP SHORT EXIT      ;                                    
  181. FLOAT_A ENDP